package com.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;

import com.entity.DiscussgongyipinxinxiEntity;
import com.entity.view.DiscussgongyipinxinxiView;

import com.service.DiscussgongyipinxinxiService;
import com.utils.PageUtils;
import com.utils.MPUtil;

/**
 * 工艺品信息评论表 后端接口
 *
 * 提供关于工艺品评论的增、删、改、查操作，包括分页查询、评论列表展示、评论提醒等功能。
 *
 * author: team10
 */
@RestController
@RequestMapping("/discussgongyipinxinxi")
public class DiscussgongyipinxinxiController {

    @Autowired
    private DiscussgongyipinxinxiService discussgongyipinxinxiService;

    /**
     * 后端列表分页查询接口
     *
     * @param params 请求参数
     * @param discussgongyipinxinxi 评论实体
     * @param request 请求对象
     * @return 分页查询结果
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, DiscussgongyipinxinxiEntity discussgongyipinxinxi, HttpServletRequest request){
        EntityWrapper<DiscussgongyipinxinxiEntity> ew = new EntityWrapper<DiscussgongyipinxinxiEntity>();
        // 调用服务层查询分页数据
        PageUtils page = discussgongyipinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, discussgongyipinxinxi), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 前端列表查询接口（无权限验证）
     *
     * @param params 请求参数
     * @param discussgongyipinxinxi 评论实体
     * @param request 请求对象
     * @return 查询结果
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, DiscussgongyipinxinxiEntity discussgongyipinxinxi, HttpServletRequest request){
        EntityWrapper<DiscussgongyipinxinxiEntity> ew = new EntityWrapper<DiscussgongyipinxinxiEntity>();
        // 调用服务层查询分页数据
        PageUtils page = discussgongyipinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, discussgongyipinxinxi), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 获取评论列表
     *
     * @param discussgongyipinxinxi 评论实体对象
     * @return 评论列表
     */
    @RequestMapping("/lists")
    public R list(DiscussgongyipinxinxiEntity discussgongyipinxinxi){
        EntityWrapper<DiscussgongyipinxinxiEntity> ew = new EntityWrapper<DiscussgongyipinxinxiEntity>();
        // 使用条件查询
        ew.allEq(MPUtil.allEQMapPre(discussgongyipinxinxi, "discussgongyipinxinxi"));
        return R.ok().put("data", discussgongyipinxinxiService.selectListView(ew));
    }

    /**
     * 查询评论信息
     *
     * @param discussgongyipinxinxi 评论实体对象
     * @return 查询结果
     */
    @RequestMapping("/query")
    public R query(DiscussgongyipinxinxiEntity discussgongyipinxinxi){
        EntityWrapper<DiscussgongyipinxinxiEntity> ew = new EntityWrapper<DiscussgongyipinxinxiEntity>();
        ew.allEq(MPUtil.allEQMapPre(discussgongyipinxinxi, "discussgongyipinxinxi"));
        // 获取评论视图信息
        DiscussgongyipinxinxiView discussgongyipinxinxiView = discussgongyipinxinxiService.selectView(ew);
        return R.ok("查询工艺品信息评论表成功").put("data", discussgongyipinxinxiView);
    }

    /**
     * 后端获取评论详情
     *
     * @param id 评论ID
     * @return 评论详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        DiscussgongyipinxinxiEntity discussgongyipinxinxi = discussgongyipinxinxiService.selectById(id);
        return R.ok().put("data", discussgongyipinxinxi);
    }

    /**
     * 前端获取评论详情（无权限验证）
     *
     * @param id 评论ID
     * @return 评论详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        DiscussgongyipinxinxiEntity discussgongyipinxinxi = discussgongyipinxinxiService.selectById(id);
        return R.ok().put("data", discussgongyipinxinxi);
    }

    /**
     * 后端保存评论
     *
     * @param discussgongyipinxinxi 评论实体
     * @param request 请求对象
     * @return 操作结果
     */
    @RequestMapping("/save")
    public R save(@RequestBody DiscussgongyipinxinxiEntity discussgongyipinxinxi, HttpServletRequest request){
        // 设置ID为当前时间戳 + 随机数
        discussgongyipinxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        // 插入评论数据
        discussgongyipinxinxiService.insert(discussgongyipinxinxi);
        return R.ok();
    }

    /**
     * 前端保存评论
     *
     * @param discussgongyipinxinxi 评论实体
     * @param request 请求对象
     * @return 操作结果
     */
    @RequestMapping("/add")
    public R add(@RequestBody DiscussgongyipinxinxiEntity discussgongyipinxinxi, HttpServletRequest request){
        // 设置ID为当前时间戳 + 随机数
        discussgongyipinxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        // 插入评论数据
        discussgongyipinxinxiService.insert(discussgongyipinxinxi);
        return R.ok();
    }

    /**
     * 更新评论信息
     *
     * @param discussgongyipinxinxi 评论实体
     * @param request 请求对象
     * @return 操作结果
     */
    @RequestMapping("/update")
    public R update(@RequestBody DiscussgongyipinxinxiEntity discussgongyipinxinxi, HttpServletRequest request){
        // 更新评论数据
        discussgongyipinxinxiService.updateById(discussgongyipinxinxi);
        return R.ok();
    }

    /**
     * 删除评论
     *
     * @param ids 评论ID数组
     * @return 操作结果
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        // 删除指定评论
        discussgongyipinxinxiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 评论提醒接口
     *
     * @param columnName 列名
     * @param type 提醒类型
     * @param map 请求参数
     * @param request 请求对象
     * @return 提醒结果
     */
    @RequestMapping("/remind/{columnName}/{type}")
    public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
                         @PathVariable("type") String type, @RequestParam Map<String, Object> map) {
        map.put("column", columnName);
        map.put("type", type);

        // 如果提醒类型是2，进行日期范围计算
        if (type.equals("2")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date remindStartDate = null;
            Date remindEndDate = null;
            if (map.get("remindstart") != null) {
                Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindStart);
                remindStartDate = c.getTime();
                map.put("remindstart", sdf.format(remindStartDate));
            }
            if (map.get("remindend") != null) {
                Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindEnd);
                remindEndDate = c.getTime();
                map.put("remindend", sdf.format(remindEndDate));
            }
        }

        // 根据日期范围查询评论
        Wrapper<DiscussgongyipinxinxiEntity> wrapper = new EntityWrapper<DiscussgongyipinxinxiEntity>();
        if (map.get("remindstart") != null) {
            wrapper.ge(columnName, map.get("remindstart"));
        }
        if (map.get("remindend") != null) {
            wrapper.le(columnName, map.get("remindend"));
        }

        // 获取评论数量
        int count = discussgongyipinxinxiService.selectCount(wrapper);
        return R.ok().put("count", count);
    }
}
